package com.yunduansing.demo.rpc;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.yunduansing.demo.exception.MyException;
import com.yunduansing.demo.model.account.LoginUserModel;
import com.yunduansing.demo.model.common.ResponseModel;
import com.yunduansing.demo.service.JwtService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserRpc {
    @Autowired
    private JwtService jwtService;

    @PostMapping("/rpc/user/validate")
    public ResponseModel validate(@RequestParam String token) {
        DecodedJWT jwt = JWT.decode(token);
        var username = jwt.getSubject();
        return ResponseModel.ok(jwtService.validateToken(username, token, false));
    }

    @PostMapping("/rpc/user/userinfo")
    public ResponseModel<LoginUserModel> getUserInfo(@RequestParam String token) throws MyException {
        var user=jwtService.getLoginUserBytoken(token);
        return ResponseModel.ok(user);
    }
}
